package com.project.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.CustomBean;
import com.project.mapper.ICustomMapper;
import com.project.service.ICustomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CustomService implements ICustomService {
    @Autowired
    private ICustomMapper customMapper;
    @Override
    public IPage<CustomBean> findByItem(Integer pageNo, String name, String scope) {
        QueryWrapper<CustomBean> qw = new QueryWrapper<>();
        if (name != null && !name.equals("")) {
            qw.eq("c_name",name);
        } if(scope!=null&&!scope.equals("")){
            qw.like("c_scope",scope);
        }
        return customMapper.selectPage(new Page<>(pageNo,2),qw);
    }

    @Override
    public CustomBean findById(Integer customId) {
        return customMapper.selectById(customId);
    }

    @Override
    public void updateById(Integer customId, String newHead, String newPhone, String newScope) {
        CustomBean customBean = customMapper.selectById(customId);
        customBean.setHead(newHead);
        customBean.setPhone(newPhone);
        customBean.setScope(newScope);
        customMapper.updateById(customBean);
    }

    @Override
    public CustomBean findByName(String name) {
        QueryWrapper<CustomBean> qw = new QueryWrapper<>();
        qw.eq("c_name",name);
        return customMapper.selectOne(qw);
    }

    @Override
    public void add(CustomBean custom) {
            customMapper.insert(custom);
    }

    @Override
    public List<CustomBean> findAll() {
        return customMapper.selectList(null);
    }
}
